package com.imooc.handleException;

import com.imooc.GirlException.GirlException;
import com.imooc.result.Result;
import com.imooc.result.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by chenxin on 2017/4/30.
 */
@ControllerAdvice
public class ExceptionHandle {
    public final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof GirlException) {
            GirlException girlException = (GirlException) e;
            logger.error("自定义异常", e);
            return ResultUtils.error(girlException.getCode(), girlException.getMessage());

        }
        // else {
        logger.error("系统异常", e);
        return ResultUtils.error(-2, "未知错误");
        // }
    }
}
